<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            
          </el-tab-pane>
        </el-tabs>

        <!-- 点击显示新增弹框 -->
        <el-row style="height: 60px">
        <el-button
          icon="el-icon-plus"
          size="small"
          type="primary"
          @click="hAdd"
        >新增角色</el-button>
        </el-row>

        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column label="序号" type="index"/>
          <el-table-column label="角色名称" prop="name"/>
          <el-table-column label="描述" prop="description"/>
          <el-table-column label="操作">
            <template v-slot="scope">
             <el-button size="small" type="success">分配权限</el-button>
              <el-button size="small" type="primary" @click="doEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-row style="text-align: center;">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="pageParams.pagesize"
            @current-change="hCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog :title="isEdit ? '编辑' : '新增'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog">
      <el-form ref="roleForm" :is-edit="isEdit" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col>
          <el-button size="small" @click="showDialog = false">取消</el-button>
          <el-button size="small" type="primary" @click="doAdd">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// --------------------------------------调用接口
import {getRoles, delRoles, addRole, editRole} from '@/api/settings'
export default {
  // ------------------------------------数据
  data() {
    return {
      // 区分是编辑还是添加
      isEdit: false,
      showDialog: false,
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      roleForm:{
        name: '',
        description: ''
    },
      list: [], // 角色数据
      total: 0, //总条数
    rules:{
      name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
    }
    }
  },
  // ------------------------------------页面加载发送ajax
  created(){
    this.loadRoles()
  },
  methods:{
    // ----------------------------------初始化渲染数据
    async loadRoles(){
      const res = await getRoles(this.pageParams)
      console.log('roles',res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // ----------------------------------点击切换页码
    hCurrentChange(curPage){
      // 用户点击哪个页码就打印哪个页码
      console.log(curPage)
      // 更新页码
      this.pageParams.page = curPage
      // 发送数据
      this.loadRoles()
    },
    // ----------------------------------点击显示删除弹框
    hDel(id){
      // console.log('1')
      this.$confirm('是否要删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    // ----------------------------------确认删除
    async doDel(id){
      await delRoles(id)
      // 如果删除是最后一条数据，则页码要-1，页码不能是0
      if (this.list.length === 1){
          let page = this.pageParams.page
          page--
          // 更新数据
          page = Math.max(1, page)
      }
      this.loadRoles()
      // 显示提示信息
      this.$message.success('删除成功')
    },
    // ----------------------------------添加数据
    async doAdd() {
      try {
        // 请求数据--->api/settings.js使用post发送请求
        const res = await addRole(this.roleForm)
        this.loadRoles()
        this.showDialog = false
        this.$message.success('添加成功')
        // 修改页码数：当前页码等于总页数加一 除以页码
        this.pageParams.page = Math.ceil((this.total + 1) / this.pageParams.pagesize)
      } catch (err) {
        this.$message.error('添加失败')
        console.log(err)
      }
    },
    // ----------------------------------编辑数据
    async doEdit() {
        // 请求数据--->api/settings.js使用post发送请求
        await editRole(this.roleForm)
        this.loadRoles()
        this.showDialog = false
        this.$message.success('编辑成功')
    },
    // ----------------------------------判断编辑添加
    hSubmit(){
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    // ----------------------------------封装编辑数据函数
    doEdit(row){
      this.isEdit = true
      this.roleForm = { ...row }
      this.showDialog = true
    },
    // ----------------------------------封装添加数据函数
    hAdd(){
      this.isEdit = false
      this.showDialog = true
      this.roleForm.name = this.roleForm.description = ''
    }
  }
}
</script>
